
#include "../inc/Ui/FindLocationForm.h"
#include "../inc/Ui/WebForm.h"
#include "../inc/Utility/MyParserXml.h"

using namespace Osp::Base;
using namespace Osp::Base::Collection;
using namespace Osp::Ui;
using namespace Osp::Ui::Controls;

int FindLocationForm::FormId = -1;

FindLocationForm::FindLocationForm(void) {
	arrAddress = null;
	numOfAddress = 0;
}

FindLocationForm::~FindLocationForm(void) {
}

bool FindLocationForm::Initialize() {
	Form::Construct(L"FindLocationForm");

	return true;
}

result FindLocationForm::OnInitializing(void) {
	result r = E_SUCCESS;

	// TODO: Add your initialization code here
	pLstDanhSachDiaChi = static_cast<List*> (GetControl(L"DanhSachDiaChiList"));
	if (pLstDanhSachDiaChi != null)
	{
		pLstDanhSachDiaChi->AddItemEventListener(*this);
	}

	pEditFieldDiaChi = static_cast<EditField*>(GetControl(L"DiaChiEditField"));
	this->SetSoftkeyActionId(SOFTKEY_0, ID_SOFTKEY_OK);
	this->SetSoftkeyActionId(SOFTKEY_1, ID_SOFTKEY_CANCEL);
	this->AddSoftkeyActionListener(SOFTKEY_0, *this);
	this->AddSoftkeyActionListener(SOFTKEY_1, *this);
	return r;
}

result FindLocationForm::OnTerminating(void) {
	result r = E_SUCCESS;

	// TODO: Add your termination code here
	if (arrAddress != null){
		arrAddress->RemoveAll(true);
	}
	delete arrAddress;
	return r;
}

void FindLocationForm::OnActionPerformed(const Osp::Ui::Control &source,int actionId) {

	FormManager *pFormManager = null;
	FormManager::GetInstance(pFormManager);

	switch (actionId) {
	case ID_SOFTKEY_OK:
		AppLog("Softkey Ok");

		TimKiem();


		break;

	case ID_SOFTKEY_CANCEL:
		AppLog("Softkey Cancel");

		pFormManager->SetCurrentForm(WebForm::GetFormId());

		break;
	}
}

void FindLocationForm::OnItemStateChanged(const Osp::Ui::Control &source,
		int index, int itemId, Osp::Ui::ItemStatus status) {
	if (index != -1) {
		int num = arrAddress->GetCount();
		String*par = static_cast<String*> (arrAddress->GetAt(num - 1 - index));
		pEditFieldDiaChi->SetText(par->GetPointer());
		////pEditFieldDiaChi->SetText(arrAddress[index]);
		pEditFieldDiaChi->RequestRedraw(true);
	}
}

void FindLocationForm::TimKiem() {
	if (pEditFieldDiaChi->GetTextLength() == 0) {
		MessageBox msg;
		msg.Construct("Chào bạn", "Bạn chưa nhập địa chỉ cần tìm",
				MSGBOX_STYLE_OK, 3000);
		//int i = 0;
		msg.Show();
	}else {


		//WebForm *frm = static_cast<WebForm*> (pFormParent);
		AppLog("Vao lay form mananger");
		FormManager *pFormManager = null;
		Form *pWebForm = null;
		FormManager::GetInstance(pFormManager);
		AppLog("Lay xong form manager");
		result r = pFormManager->GetForm(WebForm::GetFormId(), pWebForm);
		if (IsFailed(r)){
			AppLog("Failed rui");
			return;
		}
		AppLog("Set current form");
		pFormManager->SetCurrentForm(WebForm::GetFormId());
		AppLog("Bat dau tim kiem");
		String *DiaChi = new String(pEditFieldDiaChi->GetText());

		IList *list = new ArrayList();

		list->Add(*DiaChi);

		pWebForm->SendUserEvent(WebForm::REQUEST_FINDLOCATION, list);
		//static_cast<WebForm*>(pWebForm)->TimKiemBanDo(chuoi);
		AppLog("tim kiem xong");

	}
}

void FindLocationForm::ThemDiaChiTuFileVaoDanhSach() {
	AppLog("add diachi vao danh sach ");
	pLstDanhSachDiaChi->RemoveAllItems();
	AppLog("toi ne");
	MyParserXml *myParserXml = new MyParserXml();
	AppLog("toi ne1");
	if (arrAddress == null){
		arrAddress = new ArrayList();
		arrAddress->Construct();
	}
	AppLog("toi ne2");
	arrAddress = myParserXml->MoFile("/Home/MyFileXml.xml");
	int id[100] = { 0 };
	int i = 0;
	AppLog("toi ne2.1");
	numOfAddress = arrAddress->GetCount();

	for (int j = 0; j <= 10 && j < numOfAddress; j++) {
		String*par = static_cast<String*> (arrAddress->GetAt(numOfAddress - j
				- 1));
		AppLog("toi ne3");
		pLstDanhSachDiaChi->AddItem(par, null, null, null, id[i++]);
	}
	AppLog("toi ne4");
	delete myParserXml;
	AppLog("toi ne5");

}


